/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
/**
* This file contains a structure (class) definition, which uses program arguments and 
* GIMI. Read the code for more details :)
*/
#ifndef T_GIMI_CLIENT_H_
#define T_GIMI_CLIENT_H_

#include <string> 
#include "gimutils.h"

#include "gimi.h"

using namespace std;

struct TGimiClient{
	public:
		gimi::GIMI *gim;
		
		//// The program parameters
		bool paramSaveImage;
		char paramHubHost[50];
		int paramHubPort;
		char paramOwnHubName[50];
		char paramTargetName[50];
		int paramDebugLevel;
		

		TGimiClient(){
			debugInit();
			gim = new gimi::GIMI(1);			
			/// Set default parameters
			sprintf(paramHubHost,"asrobo.hut.fi");
			paramHubPort = 50002;
			sprintf(paramOwnHubName,"gimi_client");
			sprintf(paramTargetName,"gimi_target");	
			paramDebugLevel = 10;
		}	
		// Clean up
		~TGimiClient(){
			delete gim;
		}
		void printParams(){
			fprintf(stderr,"----- Starting The Program with arguments: -----\n");
			fprintf(stderr,"Hub Address:\t%s\n",paramHubHost);
			fprintf(stderr,"Hub Port:\t%d\n",paramHubPort);
			fprintf(stderr,"Connecting as:\t%s\n",paramOwnHubName);
			fprintf(stderr,"Target from:\t%s\n",paramTargetName);
			fprintf(stderr,"Debugging level is %d\n",paramDebugLevel);
			fprintf(stderr,"------------------------------------------------------\n");
		}
		void subscribe(){
		//dPrint(2,"Subscribing stuff from the target %s",,paramTargetName);		
		//gim->subscribeToService(string(paramTargetName), GIMI_PROTOCOL_JPEG_WITH_TIMESTAMP,0,false);
			
		}
		
		void start_gimi(){
			printParams();
			
			/// Initialize the debugging module
			
  			debugSetGlobalDebugLvl(paramDebugLevel);
			debugSetMode(DEBUG_MODE_TIMESTAMP_HH_MM_SS_MS);
			
			// Initialize communications etc
			if(!gim->connectToHub(paramHubHost, paramHubPort, paramOwnHubName)) {
    			dPrint(1, "Connection to hub failed. Aborted.");
    			exit(1);
  			}
  			
			this->subscribe();	
		}
		void printUsage(void){
		    fprintf(stderr,"--------------------------------------------------------------------\n");
		    fprintf(stderr,"Gimi Client Structure");
		    fprintf(stderr,"To the never ending programming byrocrasy\n");
		    fprintf(stderr,"\n");
		    fprintf(stderr,"\n");
		    fprintf(stderr,"Options:\n");
		    fprintf(stderr,"[-i] The IP adress or name of the tcpHub\n");
		    fprintf(stderr,"[-p] The listen port of the tcpHub\n");
		    fprintf(stderr,"[-n] The hub name of the image client (our name)\n");
		    fprintf(stderr,"[-s] The hub name of the target\n");
		    fprintf(stderr,"[-v] Set the debug level (default 1)\n");
		    fprintf(stderr,"\n");
		    fprintf(stderr,"--------------------------------------------------------------------------\n");
		    fprintf(stderr,"\n");
	
		}

		//Parses the program startup arguments using the famous Paanaj�vi Parser loop
		void parameterParser(int argc, char *argv[]){
		    int arg;
		    
		    for(arg=1; arg < argc; ++arg)
		    {
			char *p=argv[arg];
			if (*p == 0)
			    continue;
			if (*p == '-')
			{
			    while (*++p)
			    {
				switch(*p)
				{
				    case 'i':
					sprintf(paramHubHost,"%s",*(p+1) ? p+1 :	argv[++arg] );
						//address = *(p+1) ? p+1 : argv[++arg];
					break;
				    case 'n':
					sprintf(paramOwnHubName,"%s",*(p+1) ? p+1 :	argv[++arg] );
						//name = *(p+1) ? p+1 : argv[++arg];
					break;
				    case 's':
					sprintf(paramTargetName,"%s",*(p+1) ? p+1 :	argv[++arg] );
						//server = *(p+1) ? p+1 : argv[++arg];
					break;		
				    case 'p':
					paramHubPort = atoi(*(p+1) ? p+1 :	argv[++arg]);
						//port = atoi(*(p+1) ? p+1 :	argv[++arg]);
					break;
				    case 'v':
					paramDebugLevel = atoi(*(p+1) ? p+1 : argv[++arg]);
					break;
				    case 'h':
					printUsage();
					exit(0);
					break;	
				    case '?':
					printUsage();
					exit(0);
					break;
				    default:
					printf("unknown option %s\n",p);
					printUsage();
					exit(0);
					break;	
						
				}
			    }
			}
		    }
	
		}
};

#endif
